<?php

namespace app\admin\controller\dormitory;

use app\admin\controller\auth\Department;
use app\admin\model\DormitoryDormBed;
use app\common\controller\Backend;

class Exchange extends Backend
{
    /**
     * @var \app\admin\model\DormitoryBedExchange
     */
    protected $model = null;
    protected $searchFields = 'number';
    protected $noNeedRight = ['cxSelect'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('DormitoryBedExchange');

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->alias('a')
                ->join('__STUDENT__ b', 'b.id = a.student_id')
                ->join('__DORMITORY_DORM_BED__ c', 'c.id = a.old_bed_id')
                ->where($where)
                ->count();

            $list = $this->model
                ->alias('a')
                ->join('__STUDENT__ b', 'b.id = a.student_id')
                ->join('__DORMITORY_DORM_BED__ c', 'c.id = a.old_bed_id')
                ->where($where)
                ->field('a.*,b.name student_name,b.gender,b.number student_number')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            if ($list) {

                $bed_ids = [];
                foreach ($list as $key => $value) {
                    $bed_ids[] = $value['old_bed_id'];
                    $bed_ids[] = $value['new_bed_id'];

                    $list[$key]['gender_text'] = isset(\app\admin\model\Student::$genderList[$value['gender']]) ? \app\admin\model\Student::$genderList[$value['gender']] : '';
                }

                $bed_ids = array_unique($bed_ids);

                if ($bed_ids) {
                    $bed_list = DormitoryDormBed::alias('a')
                        ->join('__DORMITORY_GARDEN__ b', 'b.number = a.garden_number', 'left')
                        ->join('__DORMITORY_BUILDING__ c', 'c.number = a.building_number and c.garden_number = a.garden_number', 'left')
                        ->field('a.id,b.name garden_name,c.name building_name,a.dorm_number,num bed_num')
                        ->whereIn('a.id', $bed_ids)
                        ->select();
                    $bed_row = [];
                    foreach ($bed_list as $key => $value) {
                        $bed_row[$value['id']] = $value;
                    }

                    foreach ($list as $key => &$value) {
                        if (isset($bed_row[$value['old_bed_id']])) {
                            $old_bed = $bed_row[$value['old_bed_id']];
                            $value['old_garden_name']   = $old_bed['garden_name'];
                            $value['old_building_name'] = $old_bed['building_name'];
                            $value['old_dorm_number']   = $old_bed['dorm_number'];
                            $value['old_bed_num']       = $old_bed['bed_num'];
                        }

                        if (isset($bed_row[$value['new_bed_id']])) {
                            $old_bed = $bed_row[$value['new_bed_id']];
                            $value['new_garden_name']   = $old_bed['garden_name'];
                            $value['new_building_name'] = $old_bed['building_name'];
                            $value['new_dorm_number']   = $old_bed['dorm_number'];
                            $value['new_bed_num']       = $old_bed['bed_num'];
                        }
                    }

                }

            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


}
